var Class = { // 全局静态类, 用于声明一个新的类并提供构造函数支持
    create: function() {
        return function() { // 返回一个函数, 代表着这个新声明的类的构造函数
            // 一个命名为initialize的函数将被这个类实现作为类的构造函数
            this.initialize.apply(this, arguments);// initialize函数将在你实例化一个变量的时候被调用执行(即上面7个步骤中的第5步)
        }
    }
}

var MyBaiduMap = Class.create(); // 类的声明

MyBaiduMap.prototype = {
    map:{},
    cbCount:0,
    curLat:0,
    curLng:0,
    initialize: function(selector) { // 构造函数
        this.map = uexBaiduMap;
        if (selector != undefined) {
            this.createMap(selector)
        }
        //todo 测试cover
        //var pos =  $(selector).offset();
        //$(selector + "Dot").css("top",pos.top + pos.height/2 -2).css("left",pos.left + pos.width/2 -2)

    },
    createMap:function (selector,initLat,initLng,zoomLevel) {
        var _this =this;
        var pos =  $(selector).offset();
        if (initLat == undefined && initLng == undefined){
            this.map.getCurrentLocation(function(error,data){
                if (initLat == undefined) { initLat = data.latitude }
                if (initLng == undefined) { initLng = data.longitude }
                if (zoomLevel == undefined) { zoomLevel = 15.5 }
                _this.map.open(pos.left,pos.top,pos.width,pos.height,initLng,initLat, function () {
                    _this.map.setZoomLevel(zoomLevel)
                    _this.autoLocation()
                });
            });
        }else{
            _this.map.open(pos.left,pos.top,pos.width,pos.height,initLng,initLat, function () {
                _this.map.setZoomLevel(zoomLevel)
                _this.autoLocation()
            });
        }
    },
    autoLocation: function (cbFunc) {
        var _this =this;
        _this.cbCount = 0;
        _this.startLocation(function (lat,lng) {
            _this.setPosition(lat,lng);
            _this.cbCount = _this.cbCount + 1;
            if (_this.cbCount == 3) {
                _this.stopLocation();
                _this.cbCount = 0;
            }
            if (cbFunc != undefined) {
                cbFunc(lat,lng)
            }
        });
    },
    startLocation: function (cbFunc) {
        var _this = this;
        _this.map.startLocation();
        _this.map.onReceiveLocation = function(data){
            data = JSON.parse(data)
            var lat = data.latitude;
            var lng = data.longitude;
            _this.curLat = lat;
            _this.curLng = lng;
            cbFunc(lat,lng);
        }
    },
    stopLocation: function () {
        this.map.stopLocation();
    },
    setPosition: function (lat,lng) {
        this.map.setCenter(lng,lat);
    },
    getPosition: function () {
        var data = this.map.getCenter();
        return {
            lat:data.latitude,
            lng:data.longitude
        }
    },
    getCurAddress:function (cbFunc){
        var _this = this;
        var data={
            longitude: _this.curLng,
            latitude: _this.curLat
        };
        _this.map.reverseGeocode(data, function(error,data) {
            showToast("getCurAddress")
            //alert(data.address);
            cbFunc(data)
        });
    },
    getAddress:function (lat,lng,cbFunc){
        var _this = this;
        var data={
            longitude: lng,
            latitude: lat
        };
        _this.map.reverseGeocode(data, function(error,data) {
            cbFunc(data)
        });
    },
    onChange:function(cbFunc){
        var _this = this
        this.map.onMapStatusChangeListener = function(data){
            data = JSON.parse(data)
            var lat = data.center.latitude;
            var lng = data.center.longitude;
            _this.curLat = lat;
            _this.curLng = lng;
            cbFunc(data)
        }
    },
    onClick:function(func){
        this.map.onMapClickListener = function (data) {
            data = JSON.parse(data)
            var lat = data.latitude;
            var lng = data.longitude;
            func(lat,lng)
        };
    },

    addDot: function (lat,lng) {
        var _this = this
        _this.setPosition(lat,lng)
        _this.map.removeMakersOverlay('["100"]')
        var param = [{
            id:"100",
            longitude:lng,
            latitude:lat
        }];
        _this.map.addMarkersOverlay(JSON.stringify(param));
    },
    close: function () {
        this.stopLocation();
        this.map.close();
    }
}





